package com.joesem.sale.client.logic.workOrder.option

import android.os.Bundle
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaxing.sale.client.R
import com.jiaxing.sale.client.databinding.FragmentTypeListBinding
import com.joesem.sale.client.base.BaseFragment
import com.joesem.sale.client.base.color
import com.joesem.sale.client.base.createViewModel
import com.joesem.sale.client.logic.workOrder.viewModel.WorkOrderViewModel
import com.joesem.sale.client.utils.click


class ReceiptContentDialog : BaseFragment<FragmentTypeListBinding>() {

    private val viewMode by createViewModel<WorkOrderViewModel>()

    companion object {
        var onSelected: (String) -> Unit = {}
    }

    private val adapter =
        object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_single_text) {
            override fun convert(holder: BaseViewHolder, item: String) {
                holder.setText(R.id.tv_title, item)
                holder.itemView.click {
                    onSelected.invoke(item)
                    requireActivity().onBackPressed()
                }
            }
        }

    override fun FragmentTypeListBinding.initView(savedInstanceState: Bundle?) {
        tvTitle.text = "服务内容"
        rvListContent.adapter = adapter
    }

    override fun FragmentTypeListBinding.initListener() {
        root.click { }
        vTop.click { requireActivity().onBackPressed() }
        btnClose.click { requireActivity().onBackPressed() }
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onCreate(owner: LifecycleOwner) {

            }

            override fun onResume(owner: LifecycleOwner) {
                showAnimation(root)
            }

            override fun onStop(owner: LifecycleOwner) {
                root.setBackgroundColor(R.color.white.color())
            }
        })
        srlListRefresh.autoRefresh()
        srlListRefresh.setOnRefreshListener {
            viewMode.workOrderParams()
        }
    }

    override fun FragmentTypeListBinding.initIntent(intent: Any?) {
        when (intent) {
            is WorkOrderViewModel.ParamsDataIntent -> {
                srlListRefresh.finishRefresh()
                intent.data?.contents?.let {
                    adapter.setList(it)
                }
            }
        }
    }
}



fun optionReceiptContent(
    contentViewId: Int,
    fm: FragmentManager,
    onSelected: (String) -> Unit
) {
    ReceiptContentDialog.onSelected = onSelected
    fm.beginTransaction().setCustomAnimations(
        R.anim.slide_in_bottom,
        R.anim.slide_out_bottom,
        R.anim.slide_in_bottom,
        R.anim.slide_out_bottom,
    ).replace(
        contentViewId,
        BaseFragment.newInstance<ReceiptContentDialog>(Bundle())
    ).addToBackStack(null).commit()
}

